/*
 * Copyright 2009 XueSong Guo.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.webwheel.database.test;

import cn.webwheel.database.PreparedStatementProvider;
import cn.webwheel.database.annotations.Select;
import cn.webwheel.database.annotations.Update;
import cn.webwheel.database.test.domain.BoardGroup;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public interface SqlUtils {

    static class IdPSProvider implements PreparedStatementProvider {

        private String id;

        IdPSProvider(String id) {
            this.id = id;
        }

        public PreparedStatement get(Connection con, String sql) throws SQLException {
            PreparedStatement stmt = con.prepareStatement(sql + " where id=?");
            stmt.setString(1, id);
            return stmt;
        }
    }

    @Select(value = "select * from boardgroup left outer join board on boardgroup.id=board.boardgroup",
    result = "*boards:board")
    List<BoardGroup> getBoardGroups() throws Exception;

    @Select("select count(*) from user")
    int getUserCount(IdPSProvider psProvider) throws Exception;

    @Select("select * from stringtable")
    String getStringTable() throws Exception;

    @Select("select * from stringtable")
    String getStringTableTranslated() throws Exception;

    @Update("insert into stringtable values(#1#)")
    void insertStringTable(String str) throws Exception;

    @Select("select count(*) from stringtable")
    int countStringTable() throws Exception;

    @Select("select count(*) from $1$")
    int countTable(String table) throws Exception;
}
